package com.yiku.listener.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.dingtalk.open.app.api.GenericEventListener;
import com.dingtalk.open.app.api.OpenDingTalkStreamClientBuilder;
import com.dingtalk.open.app.api.message.GenericOpenDingTalkEvent;
import com.dingtalk.open.app.api.security.AuthClientCredential;
import com.dingtalk.open.app.stream.protocol.event.EventAckStatus;
import com.yiku.common.DingConstant;
import com.yiku.listener.service.ApService;
import com.yiku.utils.HttpRequestUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author ldd
 * @version 1.0
 * @date 2021/3/27 18:29
 */
@Service
public class ApServiceImpl implements ApService {


    @Override
    public void start() {
        ExecutorService pool = Executors.newCachedThreadPool();
        pool.execute(new registerThread());
        pool.execute(new scheduledThread());
    }


    private class registerThread extends Thread {
        public registerThread() {
            super();
        }

        //判断是否已经注册接口.如果注册接口则先删除在重新注册 如果没注册则直接注册
        @Override
        public void run() {
            try {
                OpenDingTalkStreamClientBuilder
                        .custom()
                        .credential(new AuthClientCredential(DingConstant.APPKEY,  DingConstant.APPSECRET))
                        //注册事件监听
                        .registerAllEventListener(new GenericEventListener() {
                            public EventAckStatus onEvent(GenericOpenDingTalkEvent event) {
                                try {
                                    //事件唯一Id
                                    String eventId = event.getEventId();
                                    //事件类型
                                    String eventType = event.getEventType();
                                    //事件产生时间
                                    Long bornTime = event.getEventBornTime();
                                    //获取事件体
                                    JSONObject bizData = event.getData();
                                    //处理事件
                                    HttpRequestUtil.sendPost("http://localhost:2020/dingc/callBack", JSON.toJSONString(bizData));
//                                    HttpRequest.sendPost("http://47.111.72.153:8052/dingc/callBack", JSON.toJSONString(bizData));
                                    //消费成功
                                    return EventAckStatus.SUCCESS;
                                } catch (Exception e) {
                                    //消费失败
                                    return EventAckStatus.LATER;
                                }
                            }
                        })
                        .build().start();


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Component
    private class scheduledThread extends Thread{
        public scheduledThread() {
            super();
        }

        @SneakyThrows
        @Override
        public void run() {
            scheduleMethod();
        }
        /**
         * 定时任务方法，授信到期提醒
         * cron属性：cron表达式：定时任务触发时间的一个字符串表达式
         */
//        @Scheduled(cron = "0 0 14 ? * MON,WED,FRI ")//在每个月的每个星期一或星期三或星期五的14:00:00秒同步
        public void scheduleMethod() {


        }
    }
}
